package com.ysqcould.web.controller.activity;

import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.json.JSONObject;
import com.ysqcould.allocation.mapper.WxConfigMapper;
import com.ysqcould.allocation.service.IWxConfigService;
import com.ysqcould.allocation.utils.UploadFileUtil;
import com.ysqcould.common.annotation.Log;
import com.ysqcould.common.core.controller.BaseController;
import com.ysqcould.common.core.domain.AjaxResult;
import com.ysqcould.common.core.page.TableDataInfo;
import com.ysqcould.common.enums.BusinessType;
import com.ysqcould.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ysqcould.activity.domain.ActivityCustom;
import com.ysqcould.activity.service.IActivityCustomService;
import org.springframework.web.multipart.MultipartFile;

import static com.ysqcould.allocation.utils.MultipartFileUtils.getFilePath;

/**
 * 活动自定义字段Controller
 * 
 * @author ruoyi
 * @date 2022-08-14
 */
@RestController
@RequestMapping("/activity/activityCustom")
public class ActivityCustomController extends BaseController
{
    @Autowired
    private IActivityCustomService activityCustomService;

    @Autowired
    private IWxConfigService wxConfigService;



    /**
     * 查询活动自定义字段列表
     */
    @PreAuthorize("@ss.hasPermi('activity:activityCustom:list')")
    @GetMapping("/list")
    public AjaxResult list(ActivityCustom activityCustom)
    {

        AjaxResult ajaxResult=new AjaxResult();
        Long userId = getUserId();
        activityCustom.setAsId(userId);
        JSONObject json = activityCustomService.selectActivityCustomList(activityCustom);
        ajaxResult.put("rows",json.get("list"));
        ajaxResult.put("status",json.get("status"));
        ajaxResult.put("fileUrlOne",json.get("fileUrlOne"));
        ajaxResult.put("fileUrlThree",json.get("fileUrlThree"));
        ajaxResult.put("fileUrlTwo",json.get("fileUrlTwo"));
        ajaxResult.put("memberRemark",json.get("memberRemark"));
        ajaxResult.put("groupRemark",json.get("groupRemark"));
        ajaxResult.put("companyRemark",json.get("companyRemark"));
        return ajaxResult;
    }

    /**
     * 个人入会上传文件
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "个人入会申请文件上传", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        final Long asId = getUserId();
        final String pictures = UploadFileUtil.uploadFileToAliOSS(asId,file);

        wxConfigService.insertWxConfigFile(pictures,asId);
        return AjaxResult.success("上传成功",pictures);
    }

    /**
     * 导出活动自定义字段列表
     */
    /*@PreAuthorize("@ss.hasPermi('activity:activityCustom:export')")
    @Log(title = "活动自定义字段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ActivityCustom activityCustom)
    {
        List<ActivityCustom> list = activityCustomService.selectActivityCustomList(activityCustom);
        ExcelUtil<ActivityCustom> util = new ExcelUtil<ActivityCustom>(ActivityCustom.class);
        util.exportExcel(response, list, "活动自定义字段数据");
    }*/

    /**
     * 获取活动自定义字段详细信息
     */
    @PreAuthorize("@ss.hasPermi('activity:activityCustom:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(activityCustomService.selectActivityCustomById(id));
    }

    /**
     * 新增活动自定义字段
     */
    @PreAuthorize("@ss.hasPermi('activity:activityCustom:add')")
    @Log(title = "活动自定义字段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActivityCustom activityCustom)
    {
        activityCustom.setAsId(getUserId());
        return toAjax(activityCustomService.insertActivityCustom(activityCustom));
    }

    /**
     * 修改活动自定义字段
     */
    @PreAuthorize("@ss.hasPermi('activity:activityCustom:edit')")
    @Log(title = "活动自定义字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ActivityCustom activityCustom)
    {
        return toAjax(activityCustomService.updateActivityCustom(activityCustom));
    }

    /**
     * 删除活动自定义字段
     */
    @PreAuthorize("@ss.hasPermi('activity:activityCustom:remove')")
    @Log(title = "活动自定义字段", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(activityCustomService.deleteActivityCustomByIds(ids));
    }
    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('activity:activityCustom:remove')")
    @Log(title = "活动自定义字段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fileName}")
    public AjaxResult delActivityCustomFile(@PathVariable Long[] ids)
    {
        return toAjax(activityCustomService.deleteActivityCustomByIds(ids));
    }
}
